<template>
  <div class="dialog">
    <el-dialog :title="titleText + '品种'" :visible.sync="showDialog" width="35%" :close-on-click-modal="false"
      @close="cancel">
      <el-form ref="ruleForm" :model="formData" :rules="rules" label-width="120px" class="demo-ruleForm">
        <el-form-item label="区域类型:" prop="areaType">
          <el-select class="w100p" filterable clearable v-model="formData.areaType" allow-create>
            <el-option v-for="item of areaType" :key="item.value + 'areaType'" :label="item.value" :value="item.name" />
          </el-select>
        </el-form-item>

        <el-form-item label="养殖类型:" prop="breedTypeNo">
          <el-select class="w100p" filterable clearable v-model="formData.breedTypeNo" allow-create>
            <el-option v-for="item of breedType" :key="item.breedTypeId + 'breedType'" :label="item.breedTypeName"
              :value="item.breedTypeNo" />
          </el-select>
        </el-form-item>

        <el-form-item label="养殖品种:" prop="fishVarietyId">
          <el-select class="w100p" filterable clearable v-model="formData.fishVarietyId" allow-create>
            <el-option v-for="item of varietyList" :key="item.fishVarietyId + 'variety'" :label="item.varietyName"
              :value="item.fishVarietyId" />
          </el-select>
        </el-form-item>
        <el-form-item label="养殖密度:" prop="breedingDensity">
          <div class="flex">
            <el-input v-model="formData.breedingDensity" clearable placeholder="请输入养殖密度"
              @change="formData.breedingDensity <= 0 ? formData.breedingDensity = '' : ''">
            </el-input>
            <el-form-item label="" class="noLabel" prop="densityUnitType">
              <el-select filterable clearable v-model="formData.densityUnitType" allow-create>
                <el-option v-for="item of densityUnitType" :key="item.value" :label="item.value" :value="item.name" />
              </el-select>
            </el-form-item>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancelbtn" @click="cancel()">取 消</el-button>
        <el-button type="primary" class="savebtn" @click="save('ruleForm')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import FishVarietyApi from "@/api/basic-info-manage/fishVariety"
import DensityApi from "@/api/basic-info-manage/breeding-density"
import BreedingTypeManageApi from "@/api/basic-info-manage/breeding-type-manage/index";
import EnumApi from "@/api/enum"

export default {
  name: '',
  components: {},
  props: {
    // 弹框名称
    titleText: {
      type: String,
      default: ""
    },
    nowChanceStau: {
      type: String,
      default: ""
    },
    show: {
      type: Boolean,
      default: false
    },
    editObject: {
      type: [Object],
      default: () => {
        return {}
      }
    }
  },
  data() {
    let checkInt = (rule, value, callback) => {
      if (value) {
        if (Number(value) && value % 1 === 0) {
          callback();
        } else {
          return callback(new Error("请输入整数！"));
        }
      }
    };
    return {
      formData: {},
      densityUnitType: [],
      areaType: [],
      breedType: [],
      varietyList: [],
      rules: {
        areaType: [
          { required: true, message: "请选择区域类型", trigger: ["change", "blur"] }
        ],
        breedTypeNo: [
          { required: true, message: "请选择养殖类型", trigger: ["change", "blur"] }
        ],
        fishVarietyId: [
          { required: true, message: "请选择养殖品种", trigger: ["change", "blur"] }
        ],
        breedingDensity: [
          { required: true, message: "请输入养殖密度", trigger: ["change", "blur"] }
        ],
        densityUnitType: [
          { required: true, message: "请选择单位", trigger: ["change", "blur"] }
        ],
      },
      showDialog: this.show
    };
  },
  computed: {
  },
  watch: {
    show(v) {
      this.showDialog = v;
    },
    nowChanceStau(v) {
      if (v === 'add') {
        this.formData = {
          // staffName: sessionStorage.getItem('staffName'),
        }
        this.$nextTick(() => {
          this.$refs['ruleForm'].clearValidate()
        })
      }
      if (v === 'edit') {
        this.formData = this.editObject
      }
    }
  },
  created() {
  },
  mounted() {
    this.queryEnum()
    this.queryVariety()
  },
  methods: {
    // 查询枚举
    queryEnum() {
      EnumApi.list({ enumType: 'DensityUnitType' }).then(res => {
        this.densityUnitType = res.data
      })
      BreedingTypeManageApi.queryByCondition().then(res => {
        this.breedType = res.data
      })
      EnumApi.list({ enumType: 'AreaType' }).then(res => {
        this.areaType = res.data
      })
    },
    // 查询品种
    queryVariety() {
      FishVarietyApi.queryByCondition().then(res => {
        this.varietyList = res.data
      })
    },
    save(formData) {
      this.$refs[formData].validate(valid => {
        if (valid && this.formData.breedingDensity > 0) {
          if (this.nowChanceStau === "edit") {
            DensityApi.edit(this.formData).then(res => {
              if (res.success === true) {
                this.$message.success("修改成功！");
                this.close()
              } else {
                this.$message.error(res.message);
              }
            });
          } else if (this.nowChanceStau === "add") {
            DensityApi.add(this.formData).then(res => {
              if (res.success === true) {
                this.$message.success("添加成功！");
                this.close()
              } else {
                this.$message.error(res.message);
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    close() {
      this.$emit("closeDialog");
    },
    cancel() {
      this.$emit("cancelDialog");
    }
  }
};
</script>

<style lang="scss" scoped>
.flex {
  display: flex;
  justify-content: flex-start;
  flex-wrap: nowrap;
}

.noLabel {
  ::v-deep .el-form-item__content {
    margin-left: 0 !important;
  }
}
</style>
